﻿
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Resources;
using System.Windows.Media.Imaging;

namespace SpaceInvaderz
{
    public enum PowerUpFunction { Health, Shields, Speed }

    public class Sprite
    {
        public Rectangle _visual;
        Point _location;
        Point _velocity;
        public Page _page;
        public Sprite _shield;
        public PowerUpFunction function;
        public int incrementor;


        public Sprite(double height, double width, string uriPath, Page page)
        {
            _page = page;
            if (uriPath == null)
                uriPath = @"SpaceInvaderz;component/Images/BlueLight.png";

            StreamResourceInfo sr1 = Application.GetResourceStream(

                new Uri(uriPath, UriKind.Relative));

            BitmapImage bmp1 = new BitmapImage();

            bmp1.SetSource(sr1.Stream);
            ImageBrush brush = new ImageBrush();
            brush.ImageSource = bmp1;
            _visual = new Rectangle();
            _visual.Width = width;
            _visual.Height = height;
            _visual.Fill = brush;
            _visual.Visibility = Visibility.Visible;
            page.myCanvas.Children.Add(_visual);
            Hp = 1;
            LifeTime = -5000;
            Hero = false;
            incrementor = 50;
        }

        public void Kill()
        {
            _visual.Visibility = Visibility.Collapsed;
            _page.myCanvas.Children.Remove(_visual);

            if (_shield != null)
            {
                _shield.Kill();
                _page.myCanvas.Children.Remove(_shield._visual);
            }

        }

        public void Update(double gameTime, double lastTime)
        {
            if (_shield != null)
            {
                if (incrementor <= 0)
                {
                    _page.ShieldUp.Stop();
                    _page.ShieldUp.Play();
                    _page.myCanvas.Children.Add(_shield._visual);
                    _shield.Hp = 1;
                    _shield._velocity = _velocity;
                    _shield._location = _location;
                    _page.gameCore._enviroment.Shields.Add(_shield);
                    _shield._visual.Visibility = Visibility.Visible;
                    if (Hero)
                        incrementor = 450;
                    else
                        incrementor = 50;
                }

                if (_shield.Hp <= 0)
                {
                    incrementor--;
                    _shield.Kill();
                    _page.myCanvas.Children.Remove(_shield._visual);
                }

                _shield.VelocityX = VelocityX;
                _shield.VelocityY = VelocityY;
                _shield._location.X = _location.X - ((_shield._visual.Width - _visual.Width) / 2);
                _shield._location.Y = _location.Y - ((_shield._visual.Height - _visual.Height) / 2);
                _shield.Update(gameTime, lastTime);
            }

            CheckBounds();
            _location.X += (gameTime - lastTime) * _velocity.X;
            _location.Y -= (gameTime - lastTime) * _velocity.Y;


            _visual.SetValue(Canvas.LeftProperty, _location.X);
            _visual.SetValue(Canvas.TopProperty, _location.Y);

            if (LifeTime != -5000)
                LifeTime--;
            if (LifeTime <= 0 && LifeTime != -5000)
                Hp = 0;
        }

        private void CheckBounds()
        {
            if (LocationX < 0 && Hero)
            {
                LocationX = 0;
            }
            _visual.SetValue(Canvas.LeftProperty, _location.X);
            _visual.SetValue(Canvas.TopProperty, _location.Y);

            if (LocationY < 0 && Hero)
            {
                LocationY = 0;
            }
            else if (LocationY < -160)
            {
                LocationY = -160;

                if (VelocityY < 0)
                    VelocityY *= -1;
            }
            _visual.SetValue(Canvas.LeftProperty, _location.X);
            _visual.SetValue(Canvas.TopProperty, _location.Y);
            if (LocationX + _visual.Width > _page.WorldLimits.X && Hero)
            {
                LocationX = _page.WorldLimits.X - _visual.Width;
            }

            _visual.SetValue(Canvas.LeftProperty, _location.X);
            _visual.SetValue(Canvas.TopProperty, _location.Y);

            if (LocationY + _visual.Height > _page.WorldLimits.Y && Hero)
            {
                LocationY = _page.WorldLimits.Y - _visual.Height;
            }
            else if (LocationY + _visual.Height > _page.WorldLimits.Y + 160)
            {
                LocationY = _page.WorldLimits.Y - _visual.Height + 160;

                if (VelocityY > 0)
                    VelocityY *= -1;
            }

            _visual.SetValue(Canvas.LeftProperty, _location.X);
            _visual.SetValue(Canvas.TopProperty, _location.Y);

        }


        public double VelocityX
        {
            get { return _velocity.X; }
            set { _velocity.X = value; }
        }
        public double VelocityY
        {
            get { return _velocity.Y; }
            set { _velocity.Y = value; }
        }

        public double LocationX
        {
            get { return _location.X; }
            set { _location.X = value; }
        }

        public double LocationY
        {
            get { return _location.Y; }
            set { _location.Y = value; }
        }

        public int Hp
        {
            get;
            set;
        }

        public int Attack
        {
            get;
            set;
        }

        public int LifeTime
        {
            get;
            set;
        }

        public bool Hero
        {
            get;
            set;
        }


        public static bool Collision(Sprite sprite1, Sprite sprite2)
        {


            // See if the sprite rectangles overlap
            return !(sprite1._location.X >= sprite2._location.X + sprite2._visual.Width
                    || sprite1._location.X + sprite1._visual.Width <= sprite2._location.X
                    || sprite1._location.Y > sprite2._location.Y + sprite2._visual.Height
                    || sprite1._location.Y + sprite1._visual.Height < sprite2._location.Y);


        }

    }
}
